//
//  CouseSignUpNoteTableViewCell.m
//  TeacherNet
//
//  Created by slz on 2017/12/13.
//  Copyright © 2017年 bnsr. All rights reserved.
//

#import "CouseSignUpNoteTableViewCell.h"

@implementation CouseSignUpNoteTableViewCell
{
    TTCourseNoteModel * _model;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        NSInteger margin = 12;
        _noteTitle = [UILabel new];
        _noteTitle.textColor = kDark_TextColor_Default;
        _noteTitle.fontSize = kFont_Size_Middle_Title;
        [self.contentView addSubview:_noteTitle];
        [_noteTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView.mas_left).offset(margin);
            make.top.mas_equalTo(self.contentView.mas_top).offset(margin);
            make.right.mas_equalTo(self.contentView.mas_right).offset(-margin);
        }];
        
        _noteCourse = [UILabel new];
        _noteCourse.textColor = kDark_TextColor_Default;
        _noteCourse.fontSize = kFont_Size_Cotent_Nomal;
        [self.contentView addSubview:_noteCourse];
        [_noteCourse mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView.mas_left).offset(margin);
            make.top.mas_equalTo(_noteTitle.mas_bottom).offset(margin);
        }];
        
        UIImageView * imageIcon = [UIImageView new];
        imageIcon.image = [UIImage imageNamed:@"home_course_cell_user"];
        [self.contentView addSubview:imageIcon];
        [imageIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(margin);
            make.top.mas_equalTo(_noteCourse.mas_bottom).offset(margin);
            make.size.mas_equalTo(CGSizeMake(10, 10));
        }];
        
        _userName = [UILabel new];
        _userName.textColor = kDark_TextColor_Default;
        _userName.fontSize = kFont_Size_Cotent_Middle;
        [self.contentView addSubview:_userName];
        [_userName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(imageIcon.mas_right).offset(5);
            make.centerY.mas_equalTo(imageIcon.mas_centerY);
        }];
        
        _noteData = [UILabel new];
        _noteData.textColor = kLight_TextColor_Default;
        _noteData.fontSize =kFont_Size_Cotent_Middle;
        [self.contentView addSubview:_noteData];
        [_noteData mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_userName.mas_right).offset(10);
            make.centerY.mas_equalTo(imageIcon.mas_centerY);
        }];
        
        _agreeCount = [UILabel new];
        _agreeCount.textColor = [UIColor redColor];
        _agreeCount.fontSize = kFont_Size_Cotent_Middle;
        [self.contentView addSubview:_agreeCount];
        [_agreeCount mas_makeConstraints:^(MASConstraintMaker *make) {
            make.trailing.mas_equalTo(-margin);
            make.centerY.mas_equalTo(self.contentView.mas_centerY);
        }];
        
        _likeImageView = [UIImageView new];
        _likeImageView.image = [self unlikeImage];
        [self.contentView addSubview:_likeImageView];
        [_likeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(_agreeCount.mas_left).offset(-5);
            make.centerY.mas_equalTo(_agreeCount.mas_centerY);
            make.size.mas_equalTo(CGSizeMake(32, 32));
        }];
        
        UIButton * likeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:likeButton];
        [likeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.contentView.mas_right);
            make.left.mas_equalTo(_likeImageView.mas_left);
            make.centerY.mas_equalTo(self.contentView.mas_centerY);
            make.height.mas_equalTo(30);
        }];
        [likeButton addBlockForControlEvents:UIControlEventTouchUpInside block:^(id  _Nonnull sender) {
            if (_model.isAgree) return;
            if ([_delegate respondsToSelector:@selector(cellDidClickLikeButtonWithNoteId:)]){
                [self agreeAction];
                [_delegate cellDidClickLikeButtonWithNoteId:_model.noteId];
            }
            _model.isAgree = YES;
            [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
                _likeImageView.layer.transformScale = 1.7;
            } completion:^(BOOL finished) {
                _likeImageView.image = [self likeImage];;
                [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
                    _likeImageView.layer.transformScale = 0.9;
                } completion:^(BOOL finished) {
                    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
                        _likeImageView.layer.transformScale = 1;
                    } completion:^(BOOL finished) {
                    }];
                }];
            }];
        }];
    }
    
    UIView * lineView = [UIView quickLineColorView];
    [self.contentView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.and.bottom.mas_equalTo(self.contentView);
        make.height.mas_equalTo(.5);
    }];
    return self;
}

- (UIImage *)likeImage {
    static UIImage *img;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        img = [UIImage imageNamed:@"course_note_like.jpg"];
    });
    return img;
}

- (UIImage *)unlikeImage {
    static UIImage *img;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        img = [UIImage imageNamed:@"course_note_un_like.jpg"];
    });
    return img;
}

- (void)agreeAction
{
    NSInteger agreeCount = [_model.agreeCount integerValue];
    agreeCount ++;
    _model.agreeCount = [NSString stringWithFormat:@"%ld",agreeCount];
    _agreeCount.text = _model.agreeCount;
}

-(void)layoutWithModel:(TTCourseNoteModel *)model
{
    _model = model;
    _noteTitle.text = model.noteContent;
    _agreeCount.text = model.agreeCount;
    _noteCourse.text = model.courseName;
    _noteData.text = model.date;
    _userName.text = model.userName;
    _likeImageView.image = _model.isAgree ? [self likeImage] : [self unlikeImage];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
